package supermarket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/*
 * 客户端的输出输入流，用于将客户端的请求发送到服务器进行验证并且接受服务器给予的回应
 * */
public class ClientAsk {
    // 创建Socket实例
    Socket socket = null;
    // 创建输入输出流
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    DataPool datas = null;

    // 初始化流的方法
    public void refreshSocket() {
        try {
            socket = new Socket("localhost", 8888);
            // 获得输入输出流
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
        }  catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 关闭输入输出流, 每次调用后都要关闭
    public void closeSocket() {
        // 关闭流的方法
        try {
            oos.close();
            ois.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 登录请求
    public DataPool login(String name, String pwd) {
        refreshSocket();
        User user = new User(name, pwd);
        datas = new DataPool();
        datas.setUser(user);
        datas.setFlag(Flag.LOGIN);
        // 写入流
        try {
            oos.writeObject(datas);
            // 接收服务器返回值
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 新增商品的方法
    public DataPool newAdd(Commodity com) {
        refreshSocket();
        datas = new DataPool();
        datas.setCommodity(com);
        datas.setFlag(Flag.NEWADD);
        // 向服务器发送商品号进行匹配
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 查询全部商品的方法
    public DataPool allGoods() {
        refreshSocket();
        datas = new DataPool();
        datas.setFlag(Flag.ALLGOODS);
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 按编号查询商品的方法
    public DataPool oneGoods(String number) {
        refreshSocket();
        datas = new DataPool();
        datas.setOneGoods(number);
        datas.setFlag(Flag.ONEGOODS);
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 新增商品时验证商品编号是否存在的查询方法
    public DataPool search(String number) {
        refreshSocket();
        datas = new DataPool();
        datas.setSearch(number);
        datas.setFlag(Flag.SEARCH);
        // 向服务器发送商品号进行匹配
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 出库方法
    public DataPool out(String number, int num) {
        refreshSocket();
        datas = new DataPool();
        Commodity commodity = new Commodity(number, num);
        datas.setCommodity(commodity);
        datas.setFlag(Flag.OUT);
        // 向服务器发送商品号进行匹配
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 增加商品方法
    public DataPool addin(String number, int num) {
        refreshSocket();
        datas = new DataPool();
        Commodity commodity = new Commodity(number, num);
        datas.setCommodity(commodity);
        datas.setFlag(Flag.ADDIN);
        // 向服务器发送商品号进行匹配
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 扫描方法
    public DataPool scan(String number) {
        refreshSocket();
        datas = new DataPool();
        datas.setScan(number);
        datas.setFlag(Flag.SCAN);
        // 向服务器发送商品号进行匹配
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 收银员,修改数量的方法
    public DataPool modify(String number, int num) {
        refreshSocket();
        datas = new DataPool();
        Commodity commodity = new Commodity(number, num);
        datas.setCommodity(commodity);
        datas.setFlag(Flag.MODIFY);
        // 向服务器发送商品号进行匹配
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 结账方法.返回购买列表
    public DataPool accounts() {
        refreshSocket();
        datas = new DataPool();
        datas.setFlag(Flag.ACCOUNT);
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }

    // 发送vip卡号到服务器进行验证
    public DataPool vipPay(String cardNum) {
        refreshSocket();
        datas = new DataPool();
        datas.setFlag(Flag.VIPNUM);
        datas.setVipCardNum(cardNum);
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
            return datas;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
        return null;
    }
    //清空购买集合中的所有物品的方法
    public void clear(){
        refreshSocket();
        datas = new DataPool();
        datas.setFlag(Flag.CLEAR);
        try {
            oos.writeObject(datas);
            // 接受服务器的回应
            datas = (DataPool) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            closeSocket();
        }
    }

}